package com.wanxi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.SuggestModel;
import com.wanxi.service.SuggestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/8 19:55
 */
@RestController
@RequestMapping("/back/suggest")
@Api(tags = "建议信息管理",value = "Open Api")
public class SuggestController {
    @Autowired
    private SuggestService suggestService;
    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询建议信息",response = SuggestModel.class,httpMethod = "GET")
    public ResultDto findAll(SuggestModel suggestModel){
        Page<SuggestModel> page = new Page<>(suggestModel.getPage(),suggestModel.getLimit());
        QueryWrapper<SuggestModel> queryWrappers = query(suggestModel);
        IPage<SuggestModel> iPage = suggestService.page(page,queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }



    @PostMapping("/addSuggest")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增建议信息",response = SuggestModel.class,httpMethod = "POST")
    public ResultDto addSuggest(SuggestModel suggestModel){
        suggestService.save(suggestModel);
      return  ResultDto.builder().code(0).msg("新增成功！").build();
    }
    @PostMapping("/updateSuggest")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改建议信息",response = SuggestModel.class,httpMethod = "POST")
    public ResultDto updateSuggest(SuggestModel suggestModel){
        suggestService.updateById(suggestModel);
        return ResultDto.builder().code(0).msg("修改成功！").build();
    }
    @PostMapping("/deleteSuggest")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除建议信息",response = SuggestModel.class,httpMethod = "POST")
    public ResultDto deleteSuggest(SuggestModel suggestModel){
        suggestService.removeById(suggestModel.getId());
        return ResultDto.builder().code(0).build();
    }
    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询建议信息",response = SuggestModel.class,httpMethod = "GET")
    public ResultDto findById(SuggestModel suggestModel){
        SuggestModel suggest = suggestService.getById(suggestModel.getId());
        return ResultDto.builder().code(0).data(suggest).build();
    }

    private QueryWrapper<SuggestModel> query(SuggestModel suggestModel) {
        QueryWrapper<SuggestModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(suggestModel.getName()==null),"name",suggestModel.getName());
        return queryWrapper;
    }
}
